#!/bin/bash

# macOS Brainfuck IDE 编译脚本
# 使用方法: ./build.sh [debug|release|clean]

PROJECT_NAME="brainfuck_ide"
BUILD_DIR="build"
BIN_DIR="bin"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印彩色信息
print_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

# 检查依赖
check_dependencies() {
    print_step "检查编译依赖..."
    
    # 检查 CMake
    if ! command -v cmake &> /dev/null; then
        print_error "CMake 未安装，请使用以下命令安装:"
        echo "  brew install cmake"
        exit 1
    fi
    
    # 检查 C++ 编译器
    if ! command -v g++ &> /dev/null && ! command -v clang++ &> /dev/null; then
        print_error "未找到 C++ 编译器，请安装 Xcode 命令行工具:"
        echo "  xcode-select --install"
        exit 1
    fi
    
    print_info "依赖检查通过"
}

# 清理构建
clean_build() {
    print_step "清理构建文件..."
    if [ -d "$BUILD_DIR" ]; then
        rm -rf "$BUILD_DIR"
        print_info "已清理构建目录"
    else
        print_warning "构建目录不存在，无需清理"
    fi
}

# 配置项目
configure_project() {
    local build_type=$1
    print_step "配置项目 (${build_type})..."
    
    mkdir -p "$BUILD_DIR"
    cd "$BUILD_DIR"
    
    cmake -DCMAKE_BUILD_TYPE=$build_type ..
    
    if [ $? -ne 0 ]; then
        print_error "CMake 配置失败"
        cd ..
        exit 1
    fi
    
    cd ..
    print_info "项目配置完成"
}

# 编译项目
compile_project() {
    local build_type=$1
    print_step "编译项目 (${build_type})..."
    
    cd "$BUILD_DIR"
    
    # 获取 CPU 核心数用于并行编译
    local cores=$(sysctl -n hw.ncpu)
    print_info "使用 ${cores} 个核心进行编译"
    
    make -j$cores
    
    if [ $? -ne 0 ]; then
        print_error "编译失败"
        cd ..
        exit 1
    fi
    
    cd ..
    print_info "编译完成"
}

# 创建符号链接
create_symlink() {
    print_step "创建便捷启动链接..."
    
    if [ -f "$BUILD_DIR/bin/$PROJECT_NAME" ]; then
        ln -sf "$BUILD_DIR/bin/$PROJECT_NAME" "run_$PROJECT_NAME"
        chmod +x "run_$PROJECT_NAME"
        print_info "已创建启动链接: ./run_$PROJECT_NAME"
    fi
}

# 显示构建信息
show_build_info() {
    print_step "构建信息:"
    echo "   项目名称: $PROJECT_NAME"
    echo "   构建类型: $BUILD_TYPE"
    echo "   编译器: $(c++ --version | head -n1)"
    echo "   CMake版本: $(cmake --version | head -n1)"
    echo "   目标文件: $BUILD_DIR/bin/$PROJECT_NAME"
}

# 运行程序
run_program() {
    if [ -f "$BUILD_DIR/bin/$PROJECT_NAME" ]; then
        print_step "运行程序..."
        echo "==========================================="
        "$BUILD_DIR/bin/$PROJECT_NAME"
    else
        print_error "可执行文件不存在，请先编译"
    fi
}

# 主函数
main() {
    local mode=${1:-release}
    
    case $mode in
        "debug")
            BUILD_TYPE="Debug"
            ;;
        "release")
            BUILD_TYPE="Release"
            ;;
        "clean")
            clean_build
            exit 0
            ;;
        "help"|"-h"|"--help")
            echo "使用方法: $0 [debug|release|clean|help]"
            echo "  debug   - 调试构建"
            echo "  release - 发布构建 (默认)"
            echo "  clean   - 清理构建文件"
            echo "  help    - 显示此帮助信息"
            exit 0
            ;;
        *)
            print_error "未知模式: $mode"
            echo "使用: $0 help 查看使用方法"
            exit 1
            ;;
    esac
    
    print_info "开始构建 Brainfuck IDE (${BUILD_TYPE})"
    
    check_dependencies
    clean_build
    configure_project $BUILD_TYPE
    compile_project $BUILD_TYPE
    create_symlink
    show_build_info
    
    print_info "构建完成! 可执行文件: $BUILD_DIR/bin/$PROJECT_NAME"
    echo ""
    echo "运行方式:"
    echo "  ./run_$PROJECT_NAME"
    echo "  或直接: $BUILD_DIR/bin/$PROJECT_NAME"
}

# 执行主函数
main "$@"